/**
 * Created by Administrator on 2017/2/1.
 */
    function select_plus(){
         select.init();
    }

    var select;
select = {
    "numDoms": null,
    "allSelectBtn": null,
    "SelectElm": null,
    "is_create": false,
    init: function () {
        _this = this;
        _this.SelectElm = document.getElementsByClassName("imglist");
        _this.createLayout();
        _this.selectAll();
        _this.selectElm();
        _this.confirm();
    },
    createLayout: function () {
        if (_this.is_create) {
            return;
        }
        var teml = template.tpl_select_wegit;
        document.body.innerHTML += teml;
        _this.numDoms = document.getElementsByClassName("select1ed")[0].getElementsByTagName('span')[0];
        _this.allSelectBtn = document.getElementsByClassName('select_inp')[0];
        _this.is_create = true;
        for (var i = 0; i < _this.SelectElm.length; i++) {
            var selbox = document.createElement("div");
            selbox.className = 'sel_box';
            selbox.id = _this.SelectElm[i].getAttribute("daid");
            _this.SelectElm[i].appendChild(selbox);
        }
    },
    selectAll: function () {
        var is_sel = false;
        var selbox = document.getElementsByClassName("sel_box");
        var sel_inp = document.getElementById('sel_inp');
        _this.allSelectBtn.onclick = function () {
            if (is_sel) {
                this.className = 'select_inp';
                selClassname = "sel_box";
                is_sel = false;
                num = 0;
            } else {
                this.className = 'select_inp select_inped';
                selClassname = 'sel_box seled'
                is_sel = true;
                num = selbox.length;
            }

            _this.numDoms.innerHTML = num;
            for (var i = 0; i < selbox.length; i++) {
                if (is_sel) {
                    id = selbox[i].getAttribute("id");
                    sel_inp.value += id + ",";
                } else {
                    sel_inp.value = '';
                }
                selbox[i].className = selClassname;
            }
        }
    },
    selectElm: function () {
        var selbox = document.getElementsByClassName("sel_box");
        var selNum = 0;
        for (var i = 0; i < selbox.length; i++) {
            selbox[i].onclick = function () {
                var id = this.getAttribute("id");
                var sel_inp = document.getElementById('sel_inp');
                if (this.className == "sel_box") {
                    sel_inp.value += id + ",";
                    this.className = "sel_box seled";
                    selNum++;
                    _this.numDoms.innerHTML = parseInt(_this.numDoms.innerHTML) + 1;
                } else {
                    var sel_inp_con = sel_inp.value;
                    sel_inp.value = sel_inp_con.replace(id + ",", '');
                    this.className = "sel_box";
                    selNum--;
                    _this.numDoms.innerHTML = parseInt(_this.numDoms.innerHTML) - 1;
                }
                _this.allSelectBtn.className = 'select_inp';
                if (selNum == selbox.length) {
                    _this.allSelectBtn.className = 'select_inp select_inped';
                }
            }
        }
    },
    confirm: function () {
        var cofirm_btn = document.getElementsByClassName('cofirm_btn')[0];
        var sel_inp = document.getElementById('sel_inp');
        cofirm_btn.onclick = function () {
            var selid = sel_inp.value.replace(/,/g, "|");
            var url = window.location.href.substring(0, window.location.href.indexOf("admin/") + 6);
            url += "picture/delImage?id=" + selid;
            ajax({
                type: "GET",
                url: url,
                dataType: 'text',
                success: function (data) {
                    alert(data);
                }
            })
            _this.removeLayout();
        }
    },
    removeLayout: function () {
        var select = document.getElementsByClassName('select1')[0];
        for (var i = 0; i < _this.SelectElm.length; i++) {
            var _selbox = _this.SelectElm[i].getElementsByClassName('sel_box')[0];
            _this.SelectElm[i].removeChild(_selbox);
        }
        document.body.removeChild(select);
        _this.is_create = false;
    }
};